<?php



namespace app\mini\model;



use think\facade\Db;

use app\common\traits\ActionRecordTrait;

use think\Model;



class CustomerCoupon extends Model

{

	use ActionRecordTrait;



    /**

     * 表名

     * @var string

     */

    protected $name = 'coupon';

    protected $deleteTime = 'deleted_at';

    protected $defaultSoftDelete = 0;



    /**

     * 获取所有优惠券列表

     * @param  array $request 请求参数

     * @return array

     * @author hz

     * @create 2021-10-22

     */

    public function getDataList($request)

    {

        try {

            $customer_id = $request['customer_id'] ?? 0;

            // $customer_id = 1;

            if(!$customer_id)

            {

            	return ['error' => '您还未认证,请先认证'];

            }



            // 优惠券列表

            $list  = Db::name('coupon')->limit(($request['page'] - 1) * $request['limit'], $request['limit'])->order('id DESC')->select()->toArray();

            if($list)

            {

                foreach ($list as &$v) {

                   $v['start_time'] = $v['start_time'] ? date('Y-m-d', $v['start_time']) : '';

                   $v['end_time'] = $v['end_time'] ? date('Y-m-d', $v['end_time']) : '';

                }

            }

            $dataCount = Db::name('coupon')->count();



            $data              = [];

            $data['list']      = $list ?? [];

            $data['dataCount'] = $dataCount ?? 0;

            return ['data' => $data];

        } catch (\Exception $e) {

            return ['error' => $e->getMessage()];

        }

    }



    /**

     * 获取客户领取的优惠券列表

     * @param  array $request 请求参数

     * @return array

     * @author hz

     * @create 2021-10-22

     */

    public function getCouponDataList($request)

    {

        try {

            $customer_id = $request['customer_id'] ?? 0;

            // $customer_id = 1;

            if(!$customer_id)

            {

                return ['error' => '您还未认证,请先认证'];

            }



            // 优惠券列表

            $where = [];

            $where = [

                ['customer_id', '=', $customer_id]

            ];

            $field = ['cs.*,cs.id as yhj_id,c.type,c.denomination,c.limit_number as service_number,c.name,c.goods_id,c.limit_number,c.use_threshold,c.start_time,c.end_time'];

            $list  = Db::name('coupon_provide_customer')->alias('cs')

                    ->join('coupon c', 'cs.coupon_id=c.id', 'LEFT')

                    ->field($field)

                    ->where($where)

                    ->limit(($request['page'] - 1) * $request['limit'], $request['limit'])->order('cs.id DESC')->select()->toArray();

            if($list)

            {

                foreach ($list as &$v) {

                   $v['start_time'] = $v['start_time'] ? date('Y-m-d', $v['start_time']) : '';

                   $v['end_time'] = $v['end_time'] ? date('Y-m-d', $v['end_time']) : '';

                }

            }

            $dataCount = count($list);



            $data              = [];

            $data['list']      = $list ?? [];

            $data['dataCount'] = $dataCount ?? 0;

            return ['data' => $data];

        } catch (\Exception $e) {

            return ['error' => $e->getMessage()];

        }

    }



    /**

     * 查看优惠券可用商品列表

     * @param  array $request 请求参数

     * @return array

     * @author hz

     * @create 2021-10-22

     */

    public function getGoodsDataList($request)

    {

        try {

            $customer_id = $request['customer_id'] ?? 0;

            $goods_id_arr = explode(',', $request['goods_id']);

            if(!$customer_id)

            {

                return ['error' => '您还未认证,请先认证'];

            }

            if(!$goods_id_arr || !$request['goods_id'])

            {

                return ['error' => '参数错误'];

            }



            // 商品列表

            $where = [];

            $where = [

                ['id', 'in', $goods_id_arr],

                ['status', '=', 1]

            ];

            $list  = Db::name('shop_goods')->where($where)->limit(($request['page'] - 1) * $request['limit'], $request['limit'])->order('id DESC')->select()->toArray();

            $dataCount = Db::name('shop_goods')->where($where)->count();



            $data              = [];

            $data['list']      = $list ?? [];

            $data['dataCount'] = $dataCount ?? 0;

            return ['data' => $data];

        } catch (\Exception $e) {

            return ['error' => $e->getMessage()];

        }

    }


    /**
     * 获取优惠券列表
     */
    public function getList($request)
    {
        try {

            $customerId = $request['id'];

            $today = date('Y-m-d',time());

            $todayT = strtotime($today);

            $where = [];

            array_push($where,['cpc.customer_id','=',$customerId]);

            array_push($where,['cpc.staus','=','0']);

            array_push($where,['c.status','<>','2']);

            array_push($where,['c.end_time','>=',$todayT]);

            $couponList = Db::name('coupon_provide_customer')->alias('cpc')
                            ->join('coupon c', 'c.id = cpc.coupon_id')
                            ->where($where)->order('cpc.id DESC')->select()->toArray();

            // $coupon_ids = array_values(array_unique(array_column($couponList,'coupon_id')));

            // $coupon_where = [];

            // array_push($where,['id','in',$coupon_ids]);

            // $list = Db::name('coupon')->where($coupon_where)->column('type,name,end_time,desc','coupon_id');

            $couponType = ['1' => '满减券','3' => '满赠券','4' => '兑换券'];

            foreach ($couponList as $key => $value) {
                $couponList[$key]['end_time'] = date("Y-m-d",$value['end_time']);

                if(!empty($value['goods_id'])){
                    $goods = explode(',',$value['goods_id']);

                    $goodsName = Db::name('shop_goods')->where('id','in',$value['goods_id'])->column('name','id');

                    $newGoods = [];

                    foreach ($goods as $k => $v) {
                        array_push($newGoods,$goodsName[$v]);
                    }

                    $couponList[$key]['goods_id'] = implode(',',$newGoods);
                }else{
                    $couponList[$key]['goods_id'] = '';
                }

                $couponList[$key]['type'] = $couponType[$value['type']];
            }

            $data              = [];

            $data['list']      = $couponList ?? [];

            $data['dataCount'] = $dataCount ?? 0;

            return ['data' => $data];
        } catch (\Throwable $e) {
            return ['error' => $e->getMessage()];
        }
    }

    public function customer_get_coupon($request){

        $nowTime = time();

        $customer_id = $request['id'];

        $where = [];

        array_push($where,['customer_id','=',$customer_id]);

        $couponList = Db::name('coupon_provide_customer')->alias('cpc')
                        ->join('coupon c', 'c.id = cpc.coupon_id')
                        ->where($where)->order('cpc.id DESC')->select()->toArray();

        $couponType = ['1' => '满减券','3' => '满赠券','4' => '兑换券'];

        foreach ($couponList as $key => $value) {

            if($value['end_time'] < $nowTime){
                // 过期
                $couponList[$key]['overdue'] = '1';

            }else{
                // 未过期
                $couponList[$key]['overdue'] = '0';

            }

            $couponList[$key]['end_time'] = date("Y-m-d",$value['end_time']);

            if(!empty($value['goods_id'])){

                $goods = explode(',',$value['goods_id']);

                $goodsName = Db::name('shop_goods')->where('id','in',$value['goods_id'])->column('name','id');

                $newGoods = [];

                foreach ($goods as $k => $v) {
                    array_push($newGoods,$goodsName[$v]);
                }

                $couponList[$key]['goods_id'] = implode(',',$newGoods);

            }else{
                
                $couponList[$key]['goods_id'] = '';

            }

            $couponList[$key]['type'] = $couponType[$value['type']];

            if($value['use_time']>0){

                $couponList[$key]['is_used'] = '1';

                $couponList[$key]['use_time'] = date('Y-m-d',$value['use_time']);

            }else{

                $couponList[$key]['is_used'] = '0';

                $couponList[$key]['use_time'] = '';

            }

        }

        // echo "<pre>";
        // print_r($couponList);
        // echo "</pre>";
        // exit;

        $data              = [];

        $data['list']      = $couponList;

        return $data;

    }

}